#include <bits/stdc++.h>
using namespace std;
int n, k, f[500100], l[500100];
int find(int x)
{
	if(f[x] == x)
		return x;
	return f[x] = find(f[x]);
}
int main()
{
	cin >> n >> k;
	for(int i = 1; i <= n; i++)
		f[i] = i;
	for(int i = 1; i <= n; i++)
		l[i] = i;
	int ans = 0;
	for(int i = 1; i <= k; i++)
	{
		int c, x, y;
		cin >> c >> x >> y;
		if(c == 1)
		{
			if((x > n || y > n) || find(x) != find(y) || l[x] != x || l[y] != y)
				++ans;
			else
				f[x] = y;
		}
		else if(c == 2)
		{
			if((x > n || y > n) || (x == y) || find(x) == find(y) || l[x] == y)
				++ans;
			else
				l[y] = x;
		}
	}
	cout << ans << endl;
	return 0;
}